<template>
  <div class="post-detail">
    <!-- 动态内容 -->
    <p class="page-title">帖子详情</p>
    <el-card class="post-card" v-if="currDetailData">
      <div class="post-header">
        <el-avatar :src="currDetailData.user.pic" class="post-avatar"></el-avatar>
        <div class="post-user-info">
          <span class="post-username">{{ currDetailData.user.nickName }}</span>
          <span class="post-time">{{ timestampToDayHM(currDetailData.updatedAt) }}</span>
        </div>
      </div>
      <div class="post-content">
        <p>{{ currDetailData.content }}</p>
        <!-- <el-image
          v-if="currDetailData.picList"
          :src="currDetailData.picList"
          :preview-src-list="[currDetailData.picList]"
          class="post-image"
        ></el-image> -->
        <el-image
            class="post-image"
            style="width:100px; height:100px;"
            v-for="(image, index) in currDetailData.picList" 
            :key="index"
            :src='image'
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="currDetailData.picList"
            show-progress
            fit="cover"
        />
      </div>
      <div class="post-actions">
          <el-button v-if="currDetailData.isLiked" type="primary" icon="ArrowUpBold" @click="toggleLike()">
              {{ currDetailData.likes }}点赞
          </el-button>
          <el-button v-else type="primary" plain icon="ArrowUpBold" @click="toggleLike()">
              {{ currDetailData.likes }}点赞
          </el-button>
          <el-button type="danger" plain icon="ChatLineSquare">
              {{ currDetailData.commentCount }} 评论
          </el-button>
      </div>
    </el-card>

    <!-- 评论列表 -->
    <el-card class="comments-card">
      <h2 class="comments-title">评论</h2>
      <div class="comment-list">
        <div v-for="comment in commentList" :key="comment.id" class="comment-item">
          <div class="comment-author">
            <el-avatar shape="square" size="medium" :src="comment.user.pic" style="float: left; margin-right: 10px;"></el-avatar>
            {{ comment.user.nickName }}
            <div class="comment-date">{{ timestampToDayHM(comment.updatedAt) }}</div>
          </div>
          <div class="comment-content">{{ comment.content }}</div>
        </div>
      </div>
      <p class="no-data" v-if="commentList.length==0">暂无评论</p>
    </el-card>

    <!-- 发表评论 -->
    <el-card class="add-comment-card">
      <h2 class="add-comment-title">发表评论</h2>
      <el-form label-width="130px" :model="editForm" :rules="rules" ref="formRef">
        <el-input
          v-model="editForm.content"
          :rules="rules"
          type="textarea"
          :rows="4"
          placeholder="请输入你的评论"
        ></el-input>
        <el-button type="info" class="submit-comment-btn" icon="Edit" @click="submitForm" style="background-color:#0038a7;">提交评论</el-button>
      </el-form>
    </el-card>
  </div>
</template>
  
<script setup lang="ts">
  import { ref, reactive, nextTick, toRef } from "vue";
  import baseService from "@/service/baseService";
  import { ElMessage, ElMessageBox } from "element-plus";
  import { timestampToDayHM, timestampToHM, timestampToDate, formatAmount } from "@/utils/utils"
  import { fileUploader } from '@/utils/upload.js'
  import { useRouter, useRoute } from 'vue-router';
  
  const router = useRouter();
  const route = useRoute();
  // 查询参数
  const queryForm = reactive({
    deptName: "",
    pageNo: 1,
    pageSize: 10
  });
  // 列表内容数量
  const total = ref(0);
  // 列表是否加载
  const loading = ref(true);

  // 资讯数据
  const articles = ref([]);

  // 表单实例
  const formRef = ref();
  // 是否打开弹出框
  const open = ref(false);
  const openDetail = ref(false);
  const commentList = ref<any[]>([]);
  const currDetailData = ref();

  // 提交表单数据
  let editForm = toRef(
    reactive({
      newsId: '',
      content: ''
    })
  );

  // 表单验证
  const rules = ref({
    content: [{ required: true, message: "规则名称不能为空", trigger: "blur" }],
  });

  /**
   * 表单重置
   */
   function reset() {
    editForm.value = {
      newsId: '',
      content: ''
    };
    nextTick(() => {
      formRef.value?.resetFields();
    });
  }

  const getInfo = () => {
      const pid = route.query.nid
      console.log('从路由获取参数：', route.query)
      baseService.get(`/api/note/detail/${pid}`).then((res) => {
          console.log('获取详情：',res)
          currDetailData.value = res.data.note;
          commentList.value = res.data.commentList;
          loading.value = false;
      }).catch(() => {
          loading.value = false;
      });
  }

  //点赞
  const toggleLike = () => {
    const pid = route.query.nid
    baseService.get(`/api/note/like/${pid}`).then((res) => {
      loading.value = false;
      if(currDetailData.value.isLiked==0){
        currDetailData.value.isLiked=1
        currDetailData.value.likes++
        ElMessage.success('点赞成功')
      }else{
        currDetailData.value.isLiked=0
        currDetailData.value.likes--
        ElMessage.warning('已取消点赞')
      }
    }).catch(() => {
        loading.value = false;
    });
  }

  //发表评论
  function submitForm() {
    formRef.value.validate((valid: boolean) => {
      if (!valid) return;
      editForm.value.newsId = currDetailData.value.id
      baseService.post(`/api/note/comment`, editForm.value).then((res) => {
        if (res.code === 200) {
          ElMessage.success(res.msg);
          open.value = false;
          editForm.value.content=''
          getInfo();
        } else {
          ElMessage.error(res.msg);
        }
      });
    });
  }

  getInfo()

</script>

<style scoped>
.post-detail {
  max-width: 800px;
  margin: 0 auto;
}

.post-card {
  margin-bottom: 20px;
}

.post-header {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.post-avatar {
  margin-right: 10px;
}

.post-user-info {
  display: flex;
  flex-direction: column;
}

.post-username {
  font-weight: bold;
}

.post-time {
  font-size: 12px;
  color: #999;
  padding-top: 5px;
}

.post-content {
  margin-bottom: 10px;
  line-height: 24px;
  white-space: break-spaces;
}

.post-image {
  width: 100%;
  margin-top: 10px;
}

.post-actions {
  display: flex;
  justify-content: flex-start;
}

.el-button {
  margin-right: 10px;
}

.comment-list {
  margin-top: 20px;
}

.comment-list h3 {
  margin-bottom: 10px;
}

.comment-card {
  margin-bottom: 10px;
}

.comment-header {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.comment-avatar {
  margin-right: 10px;
}

.comment-user-info {
  display: flex;
  flex-direction: column;
}

.comment-username {
  font-weight: bold;
}

.comment-time {
  font-size: 12px;
  color: #999;
}

.comment-content {
  margin-bottom: 10px;
}

.add-comment {
  margin-top: 20px;
}

.add-comment .el-button {
  margin-top: 10px;
}
.comments-title {
    font-size: 20px;
    margin-bottom: 15px;
    color:#555;
    font-weight: normal;
  }
  
  .comment-item {
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }
  
  .comment-author {
    font-weight: bold;
    margin-bottom: 5px;
    height: 45px;
  }
  
  .comment-content {
    font-size: 14px;
    color: #333;
    margin-bottom: 5px;
  }
  
  .comment-date {
    font-size: 12px;
    color: #999;
    padding-top: 8px;
  }
  
  .add-comment-card {
    margin-bottom: 20px;
  }
  
  .add-comment-title {
    font-size: 20px;
    margin-bottom: 15px;
    color:#555;
    font-weight: normal;
  }
  
  .submit-comment-btn {
    margin-top: 10px;
  }

  .action-buttons {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
  }
  .no-data{
    margin: 10px 0;
    color:#888;
  }
  .add-comment-card {
    margin: 20px 0;
  }
  .page-title{
    font-size: 16px;
    font-weight: bold;
    color: #0038a7;
    padding-left: 5px;
    border-left: 3px solid #0038a7;
    margin: 15px 0;
  }
</style>